<?php
class Pub {
	//数组转换成字串
	//属组序列化为字符串
	public static function arrayEval($array, $level = 0) {
		$space = '';
		for($i = 0; $i <= $level; $i++) {
			$space .= "\t";
		}
		$evaluate = "Array\n$space(\n";
		$comma = $space;
		foreach($array as $key => $val) {
			$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
			$val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
			if(is_array($val)) {
				$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
			} else {
				$evaluate .= "$comma$key => $val";
			}
			$comma = ",\n$space";
		}
		$evaluate .= "\n$space)";
		return $evaluate;
	}
	
	//写入文件
	public static function swriteFile($filename, $writetext, $openmod='w') {
		if(@$fp = fopen($filename, $openmod)) {
			flock($fp, 2);
			fwrite($fp, $writetext);
			fclose($fp);
			return true;
		} else {
			Yii::trace("写文件错误：".$filename, $writetext, 'system.CModule');
			return false;
		}
	}
	
	//人性化的时间格式
	public static function nicetime($fromtime, $totime=-1, $showtense = false, $using_timestamps=true)
	{
		if(empty($fromtime)) {
			return Yii::t('site_msg',"No date provided");
		}
	
		$periods = array('秒','分钟','小时','天','周','个月','年','旬');
		$lengths= array("60","60","24","7","4.35","12","10");
		if ($totime==-1) 
		$totime= time();
	
		// check validity of date
		if (!$using_timestamps)
			$unix_date = strtotime($fromtime, 0);
		else
			$unix_date = $fromtime;
		
		if(empty($unix_date)) {
			return '';
		}
	
		// is it future date or past date
		if($totime > $unix_date) {
			$difference = $totime - $unix_date;
			$tense = '前';
		} else {
			$difference = $unix_date - $totime;
			$tense = '刚才';
		}
		$distanceInSeconds = round(abs($totime - $fromtime));
		$distanceInMinutes = round($distanceInSeconds / 60);
			 
		if ( $distanceInMinutes <= 1 ) {
			if (!$showtense) {
				if ( $distanceInSeconds < 5 ) {
					return '5秒内';
				}
				if ( $distanceInSeconds < 10 ) {
					return '10秒内';
				}
				if ( $distanceInSeconds < 20 ) {
					return '20秒内';
				}
				if ( $distanceInSeconds < 40 ) {
					return '30秒内';
				}
				if ( $distanceInSeconds < 60 ) {
					return '1分钟内';
				}
				return '1分钟';
			}
			else
			return '刚才';
		}
	
		for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
			$difference /= $lengths[$j];
		}
	
		$difference = round($difference);
	
	//	if($difference != 1) {
	//		$periods[$j].= "s";
	//	}
		if ($showtense)
		return "$difference$periods[$j]{$tense}";
		else
		return "$difference$periods[$j]";
	}
	
	//获取时间格式
	public static function getTime($time, $formate='')
	{
		if ($formate=='')
			$formate = Yii::app()->params['timeFormate'];
		if (empty($time))
			return '';
		else
			return date($formate, $time);
	}
	
	//获取时间格式
	public static function getDate($time, $formate='')
	{
		if ($formate=='')
			$formate = Yii::app()->params['dateFormate'];
		if (empty($time))
			return '';
		else
			return date($formate, $time);
	}
	
	//获取随机数
	public static function random($length, $numeric = 0) {
		PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
		if($numeric) {
			$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
		} else {
			$hash = '';
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
			$max = strlen($chars) - 1;
			for($i = 0; $i < $length; $i++) {
				$hash .= $chars[mt_rand(0, $max)];
			}
		}
		return $hash;
	}
	
	//获取文件扩展名
	public static function getFileExt($filename)
	{
		return strtolower(trim(substr(strrchr($filename, '.'), 1)));
	}
}